﻿using UnityEngine;
using System.Collections;

public class ViewportHolder : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnDrop(GameObject droppedObject)
	{
		BarrierObjectController barrierObj = droppedObject.GetComponent<BarrierObjectController>();
		
		if( barrierObj != null)
		{
			Destroy(droppedObject);
			
			RecreateBarrierObject();
			CreateActiveBarrier(droppedObject.transform);
		}
		
	}
	
	
	public Object barrierObjectPrefab;
	public Object activeBarrierPrefab;
	
	public GameObject barrierContainer;
	
	public int barrierCount = 0;
	
	void RecreateBarrierObject()
	{
		Transform newBarrierTrans = NGUITools.AddChild(barrierContainer, barrierObjectPrefab as GameObject).transform;
		
		newBarrierTrans.localPosition = Vector3.zero;	
		
		StartCoroutine(newBarrierTrans.GetComponent<BarrierObjectController>().Cooldown((barrierCount+1)*3));
	}
	
	void CreateActiveBarrier(Transform barrierObjectTrans)
	{
		Transform newActiveBarrierTrans = NGUITools.AddChild(gameObject, activeBarrierPrefab as GameObject).transform;
		
		newActiveBarrierTrans.position = barrierObjectTrans.position;
		
		barrierCount ++;
		StartCoroutine(newActiveBarrierTrans.GetComponent<ActiveBarrierController>().Build(barrierCount*2));
	}
	
	void BarrierRemoved()
	{
		barrierCount--;
	}
	
	
	
}
